from myelin.server.lib.http import ParamValidators


class PreferencesUpdater(object):
    
    def fetchParams(self, context, source, params, errors):
        return params, errors
    
    def doUpdate(self, context, source,):
    
        params, errors = self.fetchParams(context, source, {}, {})
        
        eventData = context.event.data        
        eventData.params = params
        eventData.errors = errors
        
        if errors:
            return False
        
        return self.update(context, params)
    
    def update(self, context, params):
        return False
    

class GuestPreferencesUpdater(PreferencesUpdater):
    
    def update(self, context, params):
        args = context.request.args
        session = context.session
        defaults = context.defaults
        if 'xlocale' in params:
            if params['xlocale'] != getattr(defaults,'default_locale',None):
                session.setUserAgentVar('locale',params['xlocale'])
            elif 'locale' in session.userAgentVariables:
                session.detachUserAgentVar('locale')
            args['locale'] = [params['xlocale']]
        elif 'locale' in session.userAgentVariables:
            session.detachUserAgentVar('locale')
            
        if 'xtheme' in params:
            if params['xtheme'] != getattr(defaults,'default_theme',None):
                session.setUserAgentVar('theme',params['xtheme'])
            elif 'theme' in session.userAgentVariables:
                session.detachUserAgentVar('theme')
            args['theme'] = [params['xtheme']]
        elif 'theme' in session.userAgentVariables:
            session.detachUserAgentVar('theme')
        
        return True    
    
    
class UrlGuestPreferencesUpdater(GuestPreferencesUpdater):
    
    def fetchParams(self, context, source, params, errors):
        args = context.request.args
        
        locale = ParamValidators.SingleString(
            'xlocale',
            args,
            errors,
            min=1
        )
        
        theme = ParamValidators.SingleString(
            'xtheme',
            args,
            errors,
            min=1
        )
        
        params['xlocale'] = locale
        params['xtheme'] = theme
        
        i18nRes = context.i18nResources
        if locale not in i18nRes:
            errors['xlocale'] = u'invalid-option'
            
        if theme not in i18nRes[locale].themes:
            errors['xtheme'] = u'invalid-option'
        
        return params, errors


class UserPreferencesUpdater(PreferencesUpdater):    
   
    def update(self, context, params):
        defaults = context.defaults
        account = context.account
        prefs = account.createPreferences(defaults)
        if prefs:
            data = prefs.mLocale
            if not data:
                prefs.rdf_add('mLocale',unicode(params['xlocale']))
            else:
                for i in data:
                    i.rdf_update(unicode(params['xlocale']))
            data = prefs.mTheme
            if not data:
                prefs.rdf_add('mTheme',unicode(params['xtheme']))
            else:
                for i in data:
                    i.rdf_update(unicode(params['xtheme']))
            
            prefs.rdf_flush()            
            
            # clear agent's cache
            context.server.cacheMgr.getInvalidator(
                context.session.repo
            ).invalidateAgentCache(
                account.getAgentFName()
            )
            
            return True
    
        return False
    
    
class UrlUserPreferencesUpdater(UserPreferencesUpdater):
    
    def fetchParams(self, context, source, params, errors):
        args = context.request.args
        
        locale = ParamValidators.SingleString(
            'xlocale',
            args,
            errors,
            min=1
        )
        
        theme = ParamValidators.SingleString(
            'xtheme',
            args,
            errors,
            min=1
        )
        
        params['xlocale'] = locale
        params['xtheme'] = theme
        
        i18nRes = context.i18nResources
        if locale not in i18nRes:
            errors['xlocale'] = u'invalid-option'
            return False
            
        if theme not in i18nRes[locale].themes:
            errors['xtheme'] = u'invalid-option'
            return False
        
        return params, errors
    
